package net.my.msn.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.my.msn.dao.UserDAO;
import net.my.msn.domain.User;
import net.my.msn.service.UserService;
import net.my.msn.web.forms.Registration;

@Service
public class UserServiceImpl implements UserService {

	private UserDAO userDAO;

	@Autowired
	public void setUserDao(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Transactional
	public void addUser(Registration reg) {
		User user = new User();
		user.setEnabled(true);
		user.setAuthority("ROLE_USER");
		user.setEmail(reg.getEmail());
		user.setFirstname(reg.getFirstname());
		user.setPassword(reg.getPassword());
		user.setLastname(reg.getLastname());
		user.setUsername(reg.getUsername());
		userDAO.create(user);

	}

	@Transactional
	public List<User> userList() {
		return userDAO.find();

	}

	@Transactional
	public boolean userCheck(Registration reg) {
		return userDAO.userCheck(reg);

	}

	@Transactional
	public User userOut(Long id) {
		return userDAO.read(id);

	}

	@Transactional
	public User findBySender(Long s_id) {
		return userDAO.findBySenderId(s_id);

	}

	@Transactional
	public User userByName(String username) {
		return userDAO.findByName(username);

	}

	@Transactional
	public void userAccess(Long id) {
		User user = userDAO.read(id);
		if (user.getEnabled())
			user.setEnabled(false);
		else
			user.setEnabled(true);
		userDAO.update(user);
	}

	@Transactional
	public User userByEmail(String email) {
		return userDAO.findByEmail(email);
	}
}
